import sys
import json
import os
import struct

def read_message():
    raw_length = sys.stdin.buffer.read(4)
    if len(raw_length) == 0:
        return None
    message_length = struct.unpack('@I', raw_length)[0]
    message = sys.stdin.buffer.read(message_length).decode('utf-8')
    return json.loads(message)

def send_message(message):
    encoded_message = json.dumps(message).encode('utf-8')
    sys.stdout.buffer.write(struct.pack('@I', len(encoded_message)))
    sys.stdout.buffer.write(encoded_message)
    sys.stdout.buffer.flush()

while True:
    message = read_message()
    if message is None:
        break
    file_path = message.get('filePath')
    if os.path.exists(file_path):
        os.startfile(file_path)  # Windows
        # 或 os.system(f'open "{file_path}"')  # macOS
        # 或 os.system(f'xdg-open "{file_path}"')  # Linux
        send_message({"status": "success"})
    else:
        send_message({"status": "error", "reason": "File not found"})